<?php
require_once 'helpers.php';

class DuplicatesController extends Zend_Controller_Action
{   
    public function indexAction()
    {
        $this->view->title = "Find & Remove Duplicates";
        
        $form = new DuplicatesForm();
        $form->submit->setLabel('Find Duplicates');
        $this->view->form = $form;
                
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();

            if ($form->isValid($formData)) {

                $myDuplicatesSearch = new DuplicatesSearch($form);

                session_start();                
                if (isset($_SESSION['duplicatesearch'])) {
                    unset($_SESSION['duplicatesearch']);
                }
                
                $_SESSION['duplicatesearch'] = $myDuplicatesSearch;
                //TO DO We should get our base url somehow
                header("Location: " . "/flickrocd/duplicates/status");
            }
        }
        else {
            if (isset($_SESSION['duplicatesearch'])) {
                unset($_SESSION['duplicatesearch']);
            }
            //We show the form in the default view
        }
    }
    public function statusAction()
    {
        session_start();
        if (!isset($_SESSION['duplicatesearch'])){
            header("Location: " . "/flickrocd/duplicates");
        }
        else {
            $myDuplicatesSearch = $_SESSION['duplicatesearch'];
        }

        $myDuplicatesSearch->doNext();
        
        if(!$myDuplicatesSearch->done) {        
            $this->view->status_message = $myDuplicatesSearch->getStatus();
            $this->view->title = "Finding Duplicates";
            $this->_helper->viewRenderer('status');
        }
        else {//TODO Move this to resutlsAction
            header("Location: " . "/flickrocd/duplicates/results");
        }
    }
    
    public function resultsAction()
    {
        session_start();
        if (!isset($_SESSION['duplicatesearch'])) {
            header("Location: " . "/flickrocd/duplicates");
        }
        else {
            $myDuplicatesSearch = $_SESSION['duplicatesearch'];
        }
        if(count($myDuplicatesSearch->results) > 0) {
            $this->view->results = $myDuplicatesSearch->results;
        }
        else {
            $this->view->user_message = "No duplicates found.";
        }
        $this->view->title = "Duplicates - Results";
    }
    public function deleteAction()
    {
        session_start();
        if (!isset($_SESSION['duplicatesearch'])) {
            header("Location: " . "/flickrocd/duplicates");
        }
        else {
            $myDuplicatesSearch = $_SESSION['duplicatesearch'];
        }
        
        // We hit this controller twice. Once to confirm, once to do the
        // actual deletion.
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if (isset($formData['confirmed'])){
                $idlist = array();
                foreach ($formData as $key=>$value)
                {
                    if(!(false === strpos($key, 'delete'))){
                        array_push($idlist, $value);
                    }
                }
                $this->view->user_message = $myDuplicatesSearch->deleteDuplicates($idlist);
                $this->view->title = "Deleted";
                // Clear our search data
                unset($_SESSION['duplicatesearch']);
            }
            else{
                //Show thumbs to confirm
                $idlist = array();
                foreach ($formData as $key=>$value)
                {
                    if('on' == $value){
                        array_push($idlist, $key);
                    }
                }
                $this->view->thumblist = $myDuplicatesSearch->getThumbURLList($idlist);
                 
                // Create confirmation form
                $form = new DuplicatesConfirmForm($formData);
                $this->view->title = "Confirm Deletion";
                $this->view->form = $form;              
            }
        }
        else {
            header("Location: " . "/flickrocd/duplicates");
        } 
    }
}

?>